package com.website.dayouziblog.controller;

import com.website.dayouziblog.exception.BaseErrorEnum;
import com.website.dayouziblog.exception.BaseException;
import com.website.dayouziblog.model.DO.UserDO;
import com.website.dayouziblog.model.DTO.UserDTO;
import com.website.dayouziblog.model.VO.UserVO;
import com.website.dayouziblog.model.entity.PageBean;
import com.website.dayouziblog.response.BaseResponse;
import com.website.dayouziblog.response.RespGenerator;
import com.website.dayouziblog.service.UserService;
import com.website.dayouziblog.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;

/**
 * 登录功能Controller
 */
@Api(tags = {"用户管理"})
@RestController
public class HelloController {

    //日志
    private static final Logger LOGGER = LoggerFactory.getLogger(HelloController.class);

    @Autowired
    private UserService userService;


    /**
     * 获取存储在服务器的session //TODO:后期可以使用redis存储
     *
     * @param token
     * @param session
     * @return
     */
    @ApiIgnore
    @ApiOperation("获取session")
    @PostMapping("/getSession")
    public BaseResponse getSession(@RequestParam("token") String token, HttpSession session) {
        String userName = (String) session.getAttribute(token);
        if (userName == null) {
            LOGGER.info("获取session失败，未登录");
            return RespGenerator.returnError("未登录");
        } else {
            LOGGER.info("当前登录用户：" + userName);
        }
        return RespGenerator.returnOK(userName);
    }

    /**
     * 登录功能
     *
     * @param userDTO
     * @return
     */
    @ApiOperation("登录")
    @PostMapping(value = "/login")
    public BaseResponse loginIn(@RequestBody UserDTO userDTO) {
        //判空
        if (StringUtils.isEmpty(userDTO.getUserAccount()) || StringUtils.isEmpty(userDTO.getPassword())) {
            return RespGenerator.returnError(BaseErrorEnum.BODY_NOT_MATCH);
        }
        return RespGenerator.returnOK(userService.login(userDTO));
    }

    /**
     * 注册功能
     *
     * @param userDTO
     * @return
     */
    @ApiOperation("注册")
    @PostMapping("/regist")
    public BaseResponse registIn(@RequestBody UserDTO userDTO) {
        if (StringUtils.isEmpty(userDTO.getUserAccount()) || StringUtils.isEmpty(userDTO.getUserName()) || StringUtils.isEmpty(userDTO.getPassword())) {
            return RespGenerator.returnError(BaseErrorEnum.BODY_NOT_MATCH);
        }
        return RespGenerator.returnOK(userService.registIn(userDTO));
    }

    /**
     * 注销登录功能
     *
     * @param session
     * @param response
     * @throws IOException
     */
    @ApiIgnore
    @ApiOperation("注销")
    @GetMapping("/loginOut")
    public BaseResponse loginOut(HttpSession session, HttpServletResponse response) throws IOException {
        session.invalidate();
        Cookie cookie = new Cookie("TOKEN", null);
        cookie.setMaxAge(0);
        response.addCookie(cookie);
        Cookie cookie1 = new Cookie("JSESSIONID", null);
        cookie1.setMaxAge(0);
        response.addCookie(cookie1);
        LOGGER.info("退出登录");
//        response.sendRedirect("/login.html");
        return RespGenerator.returnOK("注销成功");
    }

    /**
     * 查询所有用户
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation("查询所有用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = false, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页展示条数", required = false, defaultValue = "10")
    })
    @GetMapping("/findAllUser")
    public BaseResponse findAllUser(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize) {
        PageBean allUser = null;
        try {
            allUser = userService.findAllUser(page, pageSize);
        } catch (Exception e) {
            LOGGER.debug(e.getMessage());
            e.printStackTrace();
        }
        return RespGenerator.returnOK(allUser);
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @ApiOperation("删除用户")
    @ApiImplicitParam(name = "id", value = "用户id", required = true)
    @GetMapping("/deleteUser")
    public BaseResponse deleteUser(@RequestParam("id") String id) {
        if (StringUtils.isEmpty(id)) {
            return RespGenerator.returnError(BaseErrorEnum.BODY_NOT_MATCH);
        }
        return RespGenerator.returnOK(userService.deleteUser(id));
    }

    /**
     * 修改用户
     * @param userDTO
     * @return
     */
    @ApiOperation("修改用户")
    @PostMapping("/updateUser")
    public BaseResponse updateUser(@RequestBody UserDTO userDTO) {
        if (StringUtils.isEmpty(userDTO.getId())) {
            return RespGenerator.returnError(BaseErrorEnum.BODY_NOT_MATCH);
        }
        return RespGenerator.returnOK(userService.updateUser(userDTO));
    }



}
